package com.zmn.plat.dubbo.impl.ficocategory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.ficocategory.FicoCategoryDRO;
import com.zmn.plat.dubbo.interfaces.ficocategory.FicoCategoryListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.product.FicoCategory;
import com.zmn.plat.services.interfaces.product.FicoCategoryService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author heciqi
 * @version v1.0
 * @since 2019/01/25 21:00
 */
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class FicoCategoryListRemoteServiceImpl implements FicoCategoryListRemoteService {
    private static final String TAG = "绩效分类 dubbo 接口";

    @Autowired
    FicoCategoryService ficoCategoryService;

    /**
     * ID 查询
     *
     * @param categoryId
     * @return
     */
    @Override
    @ReadOnlyConnection
    @Cacheable(key = "'dubbo:list:fico:categ:id:'+#p0", cacheNames = "redis1h")
    public ResponseDTO<FicoCategoryDRO> getByCategoryId(Integer categoryId) {
        log.info("getByCategoryId 参数：categoryId:{},", categoryId);
        if (Objects.isNull(categoryId)) {
            log.error(String.format("[%s]参数:[%s]不能为空", TAG, "categoryId"));
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        FicoCategory category = ficoCategoryService.findByKey(categoryId);
        // 转DRO
        FicoCategoryDRO categoryDRO = BeanMapper.map(category, FicoCategoryDRO.class);
        return ResponseDTO.success(categoryDRO, GlobalConsts.SUCCESS_MSG);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    @ReadOnlyConnection
    @Cacheable(key = "'dubbo:list:fico:categ:all'", cacheNames = "redis1h")
    public ResponseDTO<List<FicoCategoryDRO>> listAll() {
        List<FicoCategory> ficoCategories = ficoCategoryService.listAll();
        // 转DRO
        List<FicoCategoryDRO> ficoCategoryDROS = BeanMapper.mapList(ficoCategories, FicoCategoryDRO.class);
        return ResponseDTO.success(ficoCategoryDROS);
    }

    @Override
    @ReadOnlyConnection
    @Cacheable(key = "'dubbo:list:fico:category:all'", cacheNames = "redis1h")
    public ResponseDTO<List<VtDTO>> listFicoCategoryVt() {

        List<FicoCategory> categories = ficoCategoryService.listAll();
        if (CollectionUtil.isNullOrEmpty(categories)) {
            return ResponseDTO.success(Collections.EMPTY_LIST);
        }

        List<VtDTO> items = new ArrayList<>();
        categories.forEach(category -> {
            items.add(new VtDTO(category.getCategId(), category.getName(), category.getStatus()));
        });

        return ResponseDTO.success(items);
    }
}
